﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using System.Data;

namespace HZ.Supervise.Web.Message
{
    public partial class MessageList : BasePage
    {
        IBLL.IBLL_MessageTypeDetail ibllMessage = new BLL.BLL_MessageTypeDetail();

        public MessageList()
        {
            this.ModuleNo = Module.预警处理.ToString();
            this.PageOperateType = OperateType.医院药品监控.ToString();
        }
        string _strSqlWhere;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                BindList();

                //MessageTypeDetail mess = (MessageTypeDetail)ViewState["query"];
                SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                //_strMessageTypeDetailId = mess.MessageDetailId;
                //_strTypeId = mess.TypeId.ToString();
                _strSqlWhere = sq.SqlStringWhere;

            }
        }



        /// <summary>
        /// 数据绑定
        /// </summary>
        void BindList()
        {

            int t = 0;
            Model.Message message = new HZ.Supervise.Model.Message();

            if (Request.QueryString["Is_Read"] == "1")//已经查看的
            {
               
                message.TypeId = "1";
                message.Name = txtName.Text.Trim();
                message.CreatTime = txtCreatTime.Text.Trim();
                message.AreaName = txtAreaName.Text.Trim();
                message.HospitalName = txtHospitalName.Text.Trim();
                message.ProductName = txtProductName.Text.Trim();
                message.CompanyName_Jy = txtCompanyName_Jy.Text.Trim();

                message.UserId = CurrentAccount.UserID.ToString();

                gvwMessageList.DataSource = ibllMessage.GetMessage(message, true, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
                gvwMessageList.DataBind();
                AspNetPager1.RecordCount = t;


            }
            else//未查看的
            {

                message.TypeId = "1";
                message.Name = txtName.Text.Trim();
                message.CreatTime = txtCreatTime.Text.Trim();
                message.AreaName = txtAreaName.Text.Trim();
                message.HospitalName = txtHospitalName.Text.Trim();
                message.ProductName = txtProductName.Text.Trim();
                message.CompanyName_Jy = txtProductName.Text.Trim();

                message.UserId = CurrentAccount.UserID.ToString();

                gvwMessageList.DataSource = ibllMessage.GetMessage(message, false, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
                gvwMessageList.DataBind();
                AspNetPager1.RecordCount = t;

            }

        }


        protected void gvwProcureCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DirectByDetail")
            {

                string sessionid = Guid.NewGuid().ToString();

                SuperviseQuery query = (SuperviseQuery)ViewState["query"];

                Session[sessionid] = query;
                string url = "MessageDetail.aspx";

                GridViewRow row = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                string _strMessageId = Convert.ToString(this.gvwMessageList.DataKeys[row.RowIndex].Value);
                string hospitalName = e.CommandArgument.ToString();
                ibllMessage.SetMessageIsRead(_strMessageId);//设置此消息为已读

                //根据MessageTypeDetailId取得where条件
                string _strSqlStringWhere = ibllMessage.GetMessageTypeDeatilSqlStringWhere(_strMessageId);
                query.SqlStringWhere = _strSqlStringWhere;
                 Response.Redirect(string.Format("{0}?sessionid={1}", url, sessionid));
               // Response.Redirect("Message.aspx?typeId=1&hospitalName=" + hospitalName);

            }
        }
        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="sString"></param>
        /// <param name="nLeng"></param>
        /// <returns></returns>
        string SubStr(string sString, int nLeng)
        {
            if (sString.Length <= nLeng)
            {
                return sString;
            }
            string sNewStr = sString.Substring(0, nLeng);
            sNewStr = sNewStr + "...";
            return sNewStr;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();

        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindList();

        }
    }
}
